package com.huobao.yunying.fragments;

import java.io.Serializable;
import java.util.List;

import com.huobao.yunying.entity.Detail_images;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.zhangying.R;
import com.mcoy.snapscrollview.McoyScrollView;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Tuwen_detail_Fragment extends BaseFragment {

	private GridView gridView;
	private List<Detail_images> images;
	private Adapter adapter;
	private Handler handler = new Handler();
	public McoyScrollView scrollView;

//	public Tuwen_detail_Fragment(List<Detail_images> images) {
//		this.images = images;
//	}

	public static Tuwen_detail_Fragment getInstant(List<Detail_images> images){
		Tuwen_detail_Fragment instance = new Tuwen_detail_Fragment();
		Bundle args = new Bundle();
		args.putSerializable("data",(Serializable) images);
		instance.setArguments(args);
		return instance;
	}

	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_gridview, null);
		this.images = (List<Detail_images>)getArguments().getSerializable("data");
		gridView = (GridView) view.findViewById(R.id.gridview);
		scrollView = (McoyScrollView)view.findViewById(R.id.scrollview);
		adapter = new Adapter(getActivity());
		gridView.setAdapter(adapter);
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				adapter.addAll(images);
				showContentView();
			}
		});
		return view;
	}
	



	private class Adapter extends QuickAdapter<Detail_images> {

		public Adapter(Context context) {
			super(context, R.layout.image_item);
		}

		@Override
		protected void convert(BaseAdapterHelper arg0, Detail_images arg1) {
			final ImageView imageView = arg0.getView(R.id.imgView);
			ImageUtils.loadimg(imageView, arg1.image_url,new OnImageLoadListener() {
				
				@Override
				public void onLoadSuccess(Drawable drawable) {
					int newWidth = ScreenUtil.getInstant().getDisWidth(context)/2;
					int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
					LayoutParams params = (LayoutParams) imageView.getLayoutParams();
					params.width = newWidth;
					params.height = newHight;
					imageView.setLayoutParams(params);
				}
			});
		}

	}
}
